package com.wuyangnju.lj.simulation.master.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wuyangnju.lj.simulation.master.model.Slave;
import com.wuyangnju.lj.simulation.master.service.ComputionService;

@SuppressWarnings("serial")
public class RegisterSlaveServlet extends HttpServlet {
	private static final Log log = LogFactory
			.getLog(RegisterSlaveServlet.class);
	private ComputionService computionService;
	{
		computionService = ComputionService.get();
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");

		String ip = req.getParameter("ip");
		int threadNumber = Integer.parseInt(req.getParameter("threadNumber"));

		log.info("new slave. ip: " + ip + " thread number: " + threadNumber);
		computionService.registerSlave(new Slave(ip, threadNumber));
		resp.getWriter().println(
				"ip: " + ip + " thread number: " + threadNumber
						+ " registered.");
		log.info("ip: " + ip + " thread number: " + threadNumber
				+ " registered.");
	}
}
